package com.wr.filter;

import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * @author 王瑞
 * @description: 处理打印
 * @date 2020/4/17  10:24
 */
public class TPMonnitotQueue {


    private TPMonnitotQueue(){}
  /*
  * TP90 和TP99的队列map
  * */
    private static ConcurrentHashMap<String, PriorityBlockingQueue<Double>> TP90queueMap =
            new ConcurrentHashMap<String, PriorityBlockingQueue<Double>>();

    private static ConcurrentHashMap<String, PriorityBlockingQueue<Double>> TP99queueMap =
            new ConcurrentHashMap<String, PriorityBlockingQueue<Double>>();


    public static PriorityBlockingQueue<Double> getTP90queueMap(String methodName){
        if(TP90queueMap.get(methodName) == null) {
            TP90queueMap.put(methodName, new PriorityBlockingQueue<Double>());
            startTpPrint(methodName);
            startQueueAutoRemove(methodName);
        }
        PriorityBlockingQueue<Double> monitorData = TP90queueMap.get(methodName);
        return monitorData;
    }


    public static PriorityBlockingQueue<Double> getTP99queueMap(String methodName){
        if(TP99queueMap.get(methodName) == null) {
            TP99queueMap.put(methodName, new PriorityBlockingQueue<Double>());
            startTpPrint(methodName);
            startQueueAutoRemove(methodName);
        }
        PriorityBlockingQueue<Double> monitorData = TP99queueMap.get(methodName);
        return monitorData;
    }

    //每个被调用的方法对应一个线程 每隔1分钟清空一次队列
    private static void startQueueAutoRemove(String methodName) {

        new Thread(() -> {
            while (true){

                try {
                    Thread.sleep(60000);
                    PriorityBlockingQueue<Double> TP99queue = TP99queueMap.get(methodName);
                    PriorityBlockingQueue<Double> TP90queue = TP90queueMap.get(methodName);
                    TP99queue.clear();
                    TP90queue.clear();


                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });


    }

    //每个被调用的方法对应一个线程 每隔5秒打印一下调用率情况统计
    private static void startTpPrint(String methodName) {

       new Thread(()->{
           try {
               Thread.sleep(5000);

               PriorityBlockingQueue<Double> TP90Data = TP90queueMap.get(methodName);
               PriorityBlockingQueue<Double> TP99Data = TP99queueMap.get(methodName);
               Double[] data = new Double[TP90Data.size()];
               Double[] data2 = new Double[TP99Data.size()];
               TP90Data.toArray(data);
               TP99Data.toArray(data2);
               List<Double> TP90List = Arrays.asList(data);
               List<Double> TP99List = Arrays.asList(data2);

               System.out.println(methodName+"的一分钟TP90耗时统计:"+TP90List);
               System.out.println(methodName+"的一分钟TP90耗时统计:"+TP99List);



           } catch (InterruptedException e) {
               e.printStackTrace();
           }

       }).start();

    }





}
